package controler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Academy;
import model.CRN;
import business.Calificacion;
import model.Grade;
import model.Subject;
import model.beans.GradeBean;
import model.beans.PlanEstudioBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import business.misc.Util;

public class GradeAction extends MappingDispatchAction {

	/* forward name="default" path="/list_grades.jsp" */

	public ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{		
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("lst_grades", model.Grade.list(str_subjectCode, str_academyCode));
		
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward add( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));  
    	
		DynaActionForm forma = (DynaActionForm)form;
		forma.initialize(mapping);
		forma.reset(mapping, request);

		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward save( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		Calificacion calificacion = null;
		int int_addGrade = 0;
		int int_editGrade = 0;
		
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
    	DynaActionForm forma = (DynaActionForm)form;
		String str_forward;
		
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");

    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	HttpSession session = request.getSession();
		PlanEstudioBean bn_planEstudio = (PlanEstudioBean)session.getAttribute("inst_contenido");
		int int_planEstudio = bn_planEstudio.getInt_planEstudio();
    	
		calificacion = new Calificacion();
		
    	if( ((Integer)forma.get("int_gradeId")).intValue() == 0 ){
    		int_addGrade = calificacion.agrega(forma.getMap(), int_planEstudio);
			if( int_addGrade > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				if( int_addGrade == -1 ){
					request.setAttribute("req_msg", getResources(request).getMessage("Grade.AverageIncorrect"));
				}else{
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				}
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			int_editGrade = calificacion.edita(forma.getMap());
			if( int_editGrade > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				if( int_editGrade == -1 ){
					request.setAttribute("req_msg", getResources(request).getMessage("Grade.AverageIncorrect"));
				}else{
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailEdit"));
				}
				str_forward = Util.FAIL+Util.EDIT;
			}
		}
    	if(str_forward == Util.SUCCESS){
    		request.setAttribute("lst_grades", model.Grade.list(str_subjectCode, str_academyCode));
    	}
    	
		return mapping.findForward(str_forward);
	}
	
	public ActionForward edit( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	int int_calId = Integer.parseInt(request.getParameter("grade_id"));
    	request.setAttribute("academia", Academy.instance(str_academyCode));
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
		DynaActionForm forma = (DynaActionForm)form;
		GradeBean bn_grade = Grade.instance(int_calId, str_academyCode, str_subjectCode);
		forma.set("int_gradeId", new Integer(bn_grade.getInt_gradeId()));
		forma.set("str_criterio", bn_grade.getStr_criterio());
		forma.set("int_porcentaje",  new Integer(bn_grade.getInt_porcentaje()));  
		
		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward delete( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		
		if( action_retVal == null ){
			String grade_id = request.getParameter("grade_id");
			int int_result = Grade.delete(Integer.parseInt(  grade_id ));
			MessageResources resources = getResources(request);
			String str_message = null;
			if( int_result > 0 ){
				str_message = resources.getMessage("Grades.SuccessDelete");
			}else{
				str_message = resources.getMessage("Grades.FailDelete");
			}
			request.setAttribute("req_msg", str_message);
			action_retVal = mapping.findForward(Util.DEFAULT);
		}
		
		return action_retVal;
	}
	
	public ActionForward editStudentGrade( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		String str_crncode = request.getParameter("crn");
    	String str_ciclo = request.getParameter("ciclo"); 
    	String str_alumnoId = request.getParameter("code");
    			
		request.setAttribute("bn_crn", CRN.instance(str_crncode, str_ciclo));
		request.setAttribute("lst_studentCal", Grade.studentGradeList(str_alumnoId, str_ciclo, str_crncode));
		request.setAttribute("bn_student", CRN.student(str_alumnoId));

		return mapping.findForward(Util.DEFAULT);
	}
	
	public ActionForward saveStudentGrade( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		Map mapa = new HashMap();
		
		int cont=0;
		
		String str_criterioId = null;
		String str_calificacion = null;
		String str_calId = null;
		
		String str_crncode = request.getParameter("crn");
    	String str_ciclo = request.getParameter("ciclo"); 
    	String str_alumnoId = request.getParameter("studentId");
    			
		do{
			str_criterioId = request.getParameter("int_criterioId" + cont);
			str_calificacion = request.getParameter("int_calificacion" + cont);
			str_calificacion = str_calificacion==null || str_calificacion.equals("") ? "0" :str_calificacion; 
			str_calId = request.getParameter("int_calId" + cont);
			if(str_criterioId != null && str_calificacion != null && str_calId!=null){
				mapa.put("int_criterioId" +cont, new Integer(Integer.parseInt(str_criterioId)));
				mapa.put("int_calificacion" +cont, new Integer(Integer.parseInt(str_calificacion)));
				mapa.put("int_calId" +cont, new Integer(Integer.parseInt(str_calId)));
			}else{
				break;
			}
			cont++;
		}while(str_criterioId != null && str_calificacion!=null && str_calId!=null );	
		
		if(Grade.studentGradeSave(mapa, str_alumnoId, str_ciclo, str_crncode)> 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
			request.setAttribute("bn_crn", CRN.instance(str_crncode, str_ciclo));
			request.setAttribute("lst_studentCal", Grade.studentGradeList(str_alumnoId, str_ciclo, str_crncode));
			request.setAttribute("bn_student", CRN.student(str_alumnoId));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
		}
		return mapping.findForward(Util.DEFAULT);
	}	
}
